package com.yuke.cloud.service.wmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.wmc.dto.WmsFlowDto;
import com.yuke.cloud.service.wmc.entity.WmsFlow;
import com.yuke.cloud.service.wmc.entity.WmsPurchaseOrder;
import com.yuke.cloud.service.wmc.entity.WmsPurchaseReturnOrder;
import com.yuke.cloud.service.wmc.service.WmsFlowService;
import com.yuke.cloud.service.wmc.service.WmsPurchaseOrderService;
import com.yuke.cloud.service.wmc.service.WmsPurchaseReturnOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* Created by zn on 2020/11/17.
*/
@Api(value = "WEB - WmsFlowController", tags = "处理WmsFlow相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/wms/flow")
public class WmsFlowController extends BaseController {
    @Resource
    private WmsFlowService wmsFlowService;
    @Resource
    private WmsPurchaseOrderService wmsPurchaseOrderService;
    @Resource
    private WmsPurchaseReturnOrderService wmsPurchaseReturnOrderService;



    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "wmsFlow", value = "该数据记录的POJO") @RequestBody WmsFlow wmsFlow) {
        Integer update = 0;
        //新增进货单实付,即增加进货单实付金额;新增进货退货单实付,即增加进货退货单实收金额
        if (wmsFlow.getFlowType().equals((byte)1)){
            //查
            WmsPurchaseOrder wmsPurchaseOrder = wmsPurchaseOrderService.selectByKey(wmsFlow.getPurchaseId());
            Integer payment = wmsPurchaseOrder.getPayment();
            payment+=wmsFlow.getFlowMoney();
            wmsPurchaseOrder.setPayment(payment);
            //改
            update = wmsPurchaseOrderService.update(wmsPurchaseOrder);
        }else if (wmsFlow.getFlowType().equals((byte)2)){
            //查
            WmsPurchaseReturnOrder wmsPurchaseReturnOrder = wmsPurchaseReturnOrderService.selectByKey(wmsFlow.getPurchaseReturnId());
            Integer receipts = wmsPurchaseReturnOrder .getReceipts();
            receipts+=wmsFlow.getFlowMoney();
            wmsPurchaseReturnOrder.setReceipts(receipts);
            //改
            update = wmsPurchaseReturnOrderService.update(wmsPurchaseReturnOrder);
        }else if (wmsFlow.getFlowType().equals((byte)3)){
            wmsFlow.setCreateTime(new Date());
            Integer save = wmsFlowService.save(wmsFlow);
            return WrapMapper.ok(save);
        }

        if (update>0) {
            Integer save = wmsFlowService.save(wmsFlow);
            return WrapMapper.ok(save);
        }
        return WrapMapper.ok(update);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        wmsFlowService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "wmsFlow", value = "需要修改的内容") @RequestBody WmsFlow wmsFlow) {
        wmsFlowService.update(wmsFlow);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<WmsFlow> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        WmsFlow wmsFlow = wmsFlowService.findById(id);
        return WrapMapper.ok(wmsFlow);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<WmsFlow>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<WmsFlow> list = wmsFlowService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    //条件查询收付款
    @PostMapping("/selectCondition")
    @ApiOperation(httpMethod = "POST", value = "根据ID获取具体信息")
    public Wrapper<PageInfo<WmsFlow>> selectCondition(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                      @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                      @ApiParam(name = "wmsFlowDto", value = "记录id") @RequestBody WmsFlowDto wmsFlowDto){
        PageHelper.startPage(page, size);
        List<WmsFlow> list = wmsFlowService.selectCondition(wmsFlowDto);
        PageInfo<WmsFlow> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }

    //红冲
    @PostMapping("/hongchong")
    @ApiOperation(httpMethod = "POST", value = "红冲")
    public Wrapper hongchong(@ApiParam(name = "wmsFlowDto", value = "wmsFlowDto") @RequestBody WmsFlowDto wmsFlowDto){
        WmsFlow wmsFlow = new WmsFlow();
        wmsFlow.setWmsFlowId(wmsFlowDto.getWmsFlowId());
        wmsFlow.setFlowIsDeleted(wmsFlowDto.getFlowIsDeleted());
        Integer update = wmsFlowService.update(wmsFlow);
        if (update>0){
             update = wmsFlowService.hongchong(wmsFlowDto);
             return WrapMapper.ok(update);
        }
        return WrapMapper.ok(update);
    }

    //条件查询收款
    @PostMapping("/selectFlowIn")
    @ApiOperation(httpMethod = "POST", value = "根据ID获取具体信息")
    public Wrapper<PageInfo<WmsFlowDto>> selectFlowIn(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                      @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                      @ApiParam(name = "wmsFlowDto", value = "记录id") @RequestBody WmsFlowDto wmsFlowDto){
        PageHelper.startPage(page, size);
        List<WmsFlowDto> list = wmsFlowService.selectFlowIn(wmsFlowDto);
        PageInfo<WmsFlowDto> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }


}
